package com.mql.onlinechatsystem.system.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mql.onlinechatsystem.system.entity.TbApply;
import com.mql.onlinechatsystem.system.service.ITbApplyService;
import com.mql.onlinechatsystem.system.vo.TbApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.joda.LocalDateTimeParser;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马庆林
 * @since 2023-03-10
 */
@Api("申请好友")
@RestController
@RequestMapping("/system/tbApply")
@CrossOrigin
public class TbApplyController {
    @Autowired
    private ITbApplyService iTbApplyService;
    @ApiOperation("发送新的申请")
    @PostMapping("/sendApply")
    public JSONObject sendApply(@RequestBody TbApply tbApply){
        JSONObject result=new JSONObject();
        String id = IdUtil.objectId();
        String tbapplyid = IdUtil.objectId();
        tbApply.setId(id);
        tbApply.setTbapplyid(tbapplyid);
        tbApply.setIsdelete(0);
        tbApply.setIsaccept(0);
        tbApply.setCreatetime(LocalDateTime.now());
        boolean save = iTbApplyService.save(tbApply);
        if (save){
            result.put("code",200);
            result.put("message","保存成功！");
        }else {
            result.put("code",500);
            result.put("message","保存失败！");
        }
        return result;
    }
    @ApiOperation("查询好友请求")
    @GetMapping("/getApply")
    public JSONObject getApply(TbApply tbApply){
        JSONObject result=new JSONObject();
        List<TbApplyVo> tbApplyVoList=iTbApplyService.getApply(tbApply);
        result.put("code",200);
        result.put("message","查询成功！");
        result.put("data",tbApplyVoList);
        return result;
    }
    @ApiOperation("计算新的好友请求的数量")
    @GetMapping("/getNewApplyCount")
    public JSONObject getNewApplyCount(TbApply tbApply){
        JSONObject result=new JSONObject();
        Long selectCount = iTbApplyService.getBaseMapper().selectCount(new QueryWrapper<TbApply>().eq("isaccept", 0)
                .eq("friendid", tbApply.getFriendid()));
        result.put("code",200);
        result.put("message","查询成功！");
        result.put("data",selectCount);
        return result;
    }
    @ApiOperation("添加或者拒绝")
    @PostMapping("/addOrRefuse")
    public JSONObject addOrRefuse(@RequestBody TbApply tbApply){
        JSONObject result=new JSONObject();
        iTbApplyService.addOrRefuse(tbApply);
        result.put("code",200);
        result.put("message","成功");
        return result;
    }
}
